import json
from urllib.parse import unquote
import re


def handle_request(func):
    def inner(self, request, *args, **kwargs):
        data_o = request.body
        try:
            data = json.loads(data_o)
        except Exception as e:
            if request.method == 'PUT':
                data_d = data_o.decode('utf-8')
                if '&' in data_d:
                    data = {k: unquote(v) for k, v in [item.split('=') for item in data_d.split('&')]}
                else:
                    new_data = {}
                    r = re.findall(r'name="(.*)"', data_d)
                    for i in r:
                        v = re.findall(f'.*name="{i}"\r\n\r\n(.*)-*', data_d)
                        new_data[i] = v[0].strip('\r')
                    data = new_data
            else:
                data = request.POST.dict()
        request.data = data
        return func(self, request, *args, **kwargs)

    return inner
